﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Index : System.Web.UI.Page
{
  public bool loaded = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        
       // bool loaded =false;
     
            // bool a = (bool)this.PreviousPage.Items["loaded"];
                HiddenField Loaded = new HiddenField();

                Loaded.Value = "true";

                Panel1.Controls.Add(Loaded);

                LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();


                foreach (DANH_MUC_CHINH tin in linQ.DANH_MUC_CHINHs)
                {
                    if (tin.Deleted == false)
                    {
                        UserControl_MyIndexControl2 control = (UserControl_MyIndexControl2)this.Page.LoadControl(@"~\UserControl\MyIndexControl2.ascx");
                        //control.addDanhMucCon(1);
                        control.TenDanhMuc = tin.TenDanhMucChinh;
                        control.setTextandLinkDanhMucChinh(tin.TenDanhMucChinh, @"~\RaoVat\DanhMucChinh.aspx?id=" + tin.MaDanhMucChinh);
                        var query = from danhmuccon in linQ.DANH_MUC_CONs
                                    where danhmuccon.MaDanhMucChinh == tin.MaDanhMucChinh
                                    && danhmuccon.Deleted == false
                                    select
                                        new
                                        {
                                            danhmuccon.MaDanhMucCon,
                                            danhmuccon.TenDanhMucCon
                                        };


                        //control.getData(dmc1.MaDanhMucCon);
                        foreach (var dmc in query)
                        {
                            control.addDanhMucCon(dmc.MaDanhMucCon, dmc.TenDanhMucCon);
                        }
                        control.setLinkQuangCao(@"~/QuangCao/QuangCaoDuoiMucChinh.xml");

                        // control.MaDanhMucHienDauTien = dmc1.MaDanhMucCon;
                        //System.Web.UI.WebControls.Button a = (System.Web.UI.WebControls.Button)control.FindControl(dmc1.MaDanhMucCon.ToString());

                        Panel1.Controls.Add(control);
                        if (!IsPostBack)
                        {
                            if (query.Count() >= 1)
                            {
                                var dmc1 = query.First();
                                control.getData(dmc1.MaDanhMucCon);
                            }
                        }
                    }
                   

                  

                }
            
        
        
    }
        

//         UserControl_MyIndexControl control1 = (UserControl_MyIndexControl)this.Page.LoadControl(@"~\UserControl\MyIndexControl.ascx");
//         control1.addDanhMucCon(3);
// 
//         Panel1.Controls.Add(control1);
    
    
}
